<?php
namespace app\admin\controller;

use app\common\controller\Admin;
use app\common\model\Admin as Admins;
use app\common\model\Member;
use app\common\model\Archive;
use app\common\model\AdminLoginLog;
use app\common\model\User;
use think\facade\Db;
use think\facade\View;
use think\facade\Cache;

class Index extends Admin
{
    public function index()
    {
        $this->indexData();
        return View::fetch();
    }
    
    private function indexData()
    {
        $yesterday_date = date("Y-m-d",strtotime("-1 day"));
        $yesterday_start_time = strtotime($yesterday_date." 00:00:00");
        $yesterday_end_time = strtotime(date("Y-m-d")." 00:00:00");

        $data = [];

        //昨日新增会员
        $data['yesterday_user_num'] = Db::name("user")->where([['c_time',">=",$yesterday_start_time],['c_time',"<",$yesterday_end_time]])->count();

        //昨日质押LP数量
        $data['yesterday_pledge_lp_num'] = Db::name("lp_cash_list")->where([['type',"=",1],['status',"=",1],['c_time',">=",$yesterday_start_time],['c_time',"<",$yesterday_end_time]])->sum("real_money");
        $data['yesterday_pledge_lp_num'] = number_to_normal($data['yesterday_pledge_lp_num'],6);


        //昨日解押LP数量
        $data['yesterday_rele_pledge_lp_num'] = Db::name("lp_cash_list")->where([['type',"=",2],['status',"=",1],['c_time',">=",$yesterday_start_time],['c_time',"<",$yesterday_end_time]])->sum("money");
        $data['yesterday_rele_pledge_lp_num'] = number_to_normal($data['yesterday_rele_pledge_lp_num'],6);

        //昨日提取代币数量
        $data['yesterday_cash_token_money'] = Db::name("cash_list")->where([['status',"=",1],['c_time',">=",$yesterday_start_time],['c_time',"<",$yesterday_end_time]])->sum("token_money");
        $data['yesterday_cash_token_money'] = number_to_normal($data['yesterday_cash_token_money'],6);


        $adminModel = new Admins();
        $data['admin_total'] = $adminModel->count();
        
        $memberModel = new Member();
        $data['member_total'] = $memberModel->count();

        $userModel = new User();
        $data['user_total'] = $userModel->count();
        
        $archiveModel = new Archive();
        $data['archive_total'] = $archiveModel->count();
        
        $data['comment_total'] = '???';
        
        $data['login_line_json'] = $this->adminLoginLogJson();
        
        $adminLoginLogModel = new AdminLoginLog();
        $data['login_list'] = $adminLoginLogModel->limit(8)->order('id DESC')->select();
        
        $data['system_config'] = [
            '操作系统' => PHP_OS,
            '服务器时间' => date("Y-n-j H:i:s"),
            'PHP版本号' => PHP_VERSION,
            '运行环境' => $_SERVER["SERVER_SOFTWARE"],
            'PHP运行方式' => php_sapi_name(),
            '上传附件限制' => ini_get('upload_max_filesize'),
            '执行时间限制' => ini_get('max_execution_time').'秒',
        ];
        View::assign('data', $data);
    }
    
    private function adminLoginLogJson()
    {
        $nowTime = time();
        $loginLogLineTime = strtotime(date('Ymd', strtotime("-30 day")));
        $adminLoginLogModel = new AdminLoginLog();
        $adminLoginLogData = $adminLoginLogModel->field('date_format(from_unixtime(create_time),"%Y-%m-%d") as day,COUNT(id) as nums')->where('create_time', 'between', [$loginLogLineTime, $nowTime])->group('day')->order('day asc')->select();
        
        $loginLogLineArr = [];
        $data = [];
        $labels = [];
        $loginLogLineArr['datasets'][0]['label'] = lang('login_count');
        $loginLogLineArr['datasets'][0]['fill'] = false;
        $loginLogLineArr['datasets'][0]['borderColor'] = '#4bc0c0';
        foreach ($adminLoginLogData as $k => $v){
            $data[$k] = $v['nums'];
            $labels[$k] = $v['day'];
        }
        $loginLogLineArr['datasets'][0]['data'] = $data;
        $loginLogLineArr['labels'] = $labels;
        return json_encode($loginLogLineArr);
    }
    
    public function cleanCache()
    {
        Cache::clear();
        return ajax_return(lang('action_success'), '', 1);
    }
}